package com.jeesite.modules.kj.api.controller.member;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.shiro.realms.K;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.vo.DataListPageVo;
import com.jeesite.modules.kj.api.vo.request.QuestionRequestVo;
import com.jeesite.modules.kj.api.vo.response.ActivityCommentResponseVo;
import com.jeesite.modules.kj.api.vo.response.DictResponseVo;
import com.jeesite.modules.kj.api.vo.response.QuestionDetailResponseVo;
import com.jeesite.modules.kj.api.vo.response.QuestionResponseVo;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.entity.KjActivityComment;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.entity.KjQuestion;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.service.KjQuestionService;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.DictUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:用户信息
 */
@Api(value = "问题反馈api", tags = {"问题反馈api"})
@Slf4j
@Controller
@RequestMapping(value = "/api/question")
public class QuestionApiController {

    @Autowired
    KjQuestionService kjQuestionService;

    @ApiOperation(value = "获取问题类型", notes = "获取问题类型")
    @GetMapping(value = "questionTypeList")
    public ResponseBean<List<DictResponseVo>> questionTypeList() {
        List<DictData> dictList = DictUtils.getDictList("question_type");
        List<DictResponseVo> vos = ListUtils.newArrayList();
        for (DictData dict: dictList
        ) {
            vos.add(new DictResponseVo(dict));
        }
        return new ResponseBean<>(vos);
    }

    @ApiOperation(value = "添加问题", notes = "添加评论")
    @PostMapping(value = "create")
    @UserLoginToken
    public ResponseBean<String> create(@ApiIgnore @CurrentUser String memberId, QuestionRequestVo vo) throws FreezMemberException {
        KjQuestion question = vo.convertToEntity();
        KjMember currentMember = KjMemberUtil.getCurrentMember(memberId);
        question.setKjMember(currentMember);
        question.setMemberId(currentMember.getId());
        question.setMemberName(currentMember.getName());
        question.setCity(currentMember.getAddress());
        kjQuestionService.save(question);
        return new ResponseBean<>(question.getId());
    }

    @ApiOperation(value = "我的问题列表", notes = "我的问题列表")
    @GetMapping(value = "my")
    @UserLoginToken
    public ResponseBean<DataListPageVo<QuestionResponseVo>> my(@ApiIgnore @CurrentUser String memberId, int pageNo, int pageSize) throws FreezMemberException {
        KjQuestion where = new KjQuestion();
        KjMember currentMember = KjMemberUtil.getCurrentMember(memberId);
        where.setKjMember(currentMember);
        Page page = new Page(pageNo, pageSize);
        where.setPage(page);
        Page<KjQuestion> questionPage = kjQuestionService.findPage(where);
        DataListPageVo<QuestionResponseVo> vos = new DataListPageVo<>(questionPage, QuestionResponseVo.class);
        return new ResponseBean<>(vos);
    }


    @ApiOperation(value = "问题详情", notes = "问题详情")
    @GetMapping(value = "{id}")
    @UserLoginToken
    public ResponseBean<Object> detail(@ApiIgnore @CurrentUser String memberId, @PathVariable(name = "id") String id) {
        KjQuestion kjQuestion = kjQuestionService.get(id);
        if(kjQuestion.getKjMember() != null && kjQuestion.getKjMember().getId().equals(memberId)){
            return new ResponseBean<>(new QuestionDetailResponseVo(kjQuestion));
        }else{
            return new ResponseBean<>(Global.FALSE, "没有权限查看");
        }
    }
}
